<template>
  <div>
    <div id="setMeal"></div>
    <div class="center" style="height: 340px">
      <el-row style="display: flex; justify-content: space-around">
        <el-col :span="4" v-for="item in priceData" :key="item.id">
          <el-card shadow="hover" :style="item.color">
            <div
              style="
                color: #fff;
                display: flex;
                flex-direction: column;
                align-items: center;
              "
            >
              <h1 style="margin-top: 25px; font-size: 25px">{{ item.name }}</h1>
              <div style="margin-top: 35px">
                <i style="font-size: 20px">{{ item.number }}</i
                ><i style="font-size: 14px">点</i>
              </div>
              <h2 style="margin-top: 40px; font-size: 25px">
                价格{{ item.price }}
              </h2>
              <p style="margin-top: 25px">另在额外赠送{{ item.duration }}次</p>
              <i
                style="
                  cursor: pointer;
                  padding: 5px 40px;
                  border: 1px solid #fff;
                  border-radius: 3px;
                  margin-top: 35px;
                "
                @click="propdu(String(item.id))"
                >立即购买</i
              >
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <div class="center" style="color: #999">
      <p>(注) 次数使用说明：</p>
      <p>根据包大小，每30M扣除一点下载点数</p>
    </div>
    <div style="background-color: #f5f5f5">
      <div class="center">
        <div
          style="display: flex; justify-content: space-between; padding: 30px"
        >
          <div style="color: #999">
            <h1 style="color: #74b9ff; font-size: 30px">下载套餐次数说明</h1>
            <p style="margin-top: 30px; font-size: 16px">
              1.次数无时间限制，用完为止。
            </p>
            <p style="margin-top: 10px; font-size: 16px">
              2.账号下所有应用和版本共用套餐次数。
            </p>
            <p style="margin-top: 10px; font-size: 16px">
              3.套餐可叠加购买，可申请发票
            </p>
            <p style="margin-top: 10px; font-size: 16px">
              4.凡是购买下载套餐，账户下所用应用将永久保存。
            </p>
            <p style="margin-top: 10px; font-size: 16px">
              5.平台支持3G以内大包应用，应用过大请联系客服！
            </p>
            <p style="margin-top: 10px; font-size: 16px">
              6.此套餐为下载流量次数套餐与其他业务无关，请确认后购买。
            </p>
          </div>
          <img src="../../assets/Explain.png" alt="" />
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="outerVisible" width="40%">
      <div style="display: flex; justify-content: space-around">
        <div
          style="
            background-color: #00c901;
            padding: 30px;
            display: flex;
            flex-direction: column;
            align-items: center;
          "
        >
          <img src="../../assets/Alipay/weixin.png" alt="" />
          <i
            style="
              margin-top: 30px;
              padding: 10px 30px;
              background-color: #fff;
              color: #00c901;
              border-radius: 10px;
              cursor: pointer;
            "
            @click="getCanvas('2')"
            >微信支付</i
          >
        </div>
        <div
          style="
            background-color: #0aaaf0;
            padding: 30px;
            display: flex;
            flex-direction: column;
            align-items: center;
          "
        >
          <img src="../../assets/Alipay/zhifubao.png" alt="" />
          <i
            style="
              margin-top: 30px;
              padding: 10px 30px;
              background-color: #fff;
              color: #0aaaf0;
              border-radius: 10px;
              cursor: pointer;
            "
            @click="getCanvas('1')"
            >支付宝支付</i
          >
        </div>
        <div
          style="
            background-color: #f8736e;
            padding: 30px;
            display: flex;
            flex-direction: column;
            align-items: center;
          "
        >
          <img src="../../assets/Alipay/yu_e.png" alt="" />
          <i
            style="
              margin-top: 30px;
              padding: 10px 30px;
              background-color: #fff;
              color: #f8736e;
              border-radius: 10px;
              cursor: pointer;
            "
            @click="open"
            >余额支付</i
          >
        </div>
      </div>
      <div style="text-align: center">
        温馨提示:对公打款和开票服务请联系客服 QQ：870082323
      </div>
      <el-dialog
        width="15%"
        title="请支付订单"
        :visible.sync="innerVisible"
        append-to-body
      >
        <canvas ref="qrcodeCanvas"></canvas>
        <div slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { price } from "@/api";
import { order_create, order_result } from "@/api/person";
import QRcode from "qrcode";

export default {
  watch: {
    innerVisible(value) {
      if (!value) {
        this.timer && clearInterval(this.timer);
      }
    },
  },
  data() {
    return {
      priceData: [],
      outerVisible: false,
      innerVisible: false,
      timer: null,
      Id: "",
      num: 0,
    };
  },
  async mounted() {
    const list = await price({ type: "1" });
    this.priceData = list.data.map((res) => {
      res.color = this.generateRandomGradientColor();
      return res;
    });
  },
  methods: {
    insertAlipayForm(HTML) {
      const container = document.createElement("div");
      container.innerHTML = HTML;
      document.body.appendChild(container);
      // 提交表单
      document.forms["alipaysubmit"].submit();
      // 设置延迟来移除表单，这里的延迟时间需要根据实际情况调整
      container.innerHTML = "";
      document.body.removeChild(container);
    },
    open() {
      this.$confirm("确认使用余额付款?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(async () => {
          const res = await order_create({ id: this.Id, pay_type: "6" });
          if (res.status == 0) {
            this.$message.success("支付成功");
            setTimeout(() => {
              this.$router.push("/");
            }, 1500);
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {});
    },
    propdu(id) {
      if (!this.$store.state.token) return this.$router.push("/login");
      this.Id = id;
      this.outerVisible = true;
    },
    pollding(pay_type, pay_id, res) {
      if (pay_type == "2") {
        this.innerVisible = true;
        this.$nextTick(() => {
          QRcode.toCanvas(this.$refs.qrcodeCanvas, res.data.code_url, {
            with: 200,
            color: {
              bark: "#000",
              light: "#ffff",
            },
          });
        });
        this.timer = setInterval(async () => {
          this.num++;
          if (this.num > 20) {
            this.$message.warning("支付超时"), (this.num = 0);
            this.innerVisible = false;
            clearInterval(this.timer);
            return;
          }
          const res = await order_result({ pay_id });
          if (res.data !== "未支付") {
            this.$router.push("/");
          }
        }, 3000);
      } else if (pay_type == "1") {
        this.insertAlipayForm(res.data.code_url);
      }
    },
    //二维码
    getCanvas(pay_type) {
      order_create({ id: this.Id, pay_type }).then((res) => {
        this.pollding(pay_type, res.data.order.pay_id, res);
      });
    },
    generateRandomGradientColor() {
      // 生成随机渐变颜色的逻辑
      const color1 = this.getRandomColor();
      const color2 = this.getRandomColor();
      return `background: linear-gradient(${color1}, ${color2})`;
    },
    getRandomColor() {
      // 生成随机颜色的逻辑，你可以使用自己的逻辑来生成颜色
      const letters = "0123456789ABCDEF";
      let color = "#";
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    },
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
};
</script>

<style lang="scss" scoped>
#setMeal {
  height: 200px;
  background: url("../../assets/banner5.png") no-repeat;
  background-size: 100%;
}
.center {
  width: 1500px;
  margin: 20px auto;
  i {
    color: #fff;
    &:hover {
      background-color: #fff;
      color: #333;
    }
  }
}
</style>